package com.shangma.cn.perm;

import com.shangma.cn.commons.http.AxiosStatus;
import com.shangma.cn.commons.utils.TokenService;
import com.shangma.cn.entity.base.impl.Menu;
import com.shangma.cn.exception.base.impl.ApiException;
import com.sun.el.parser.Token;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/13  17:28
 * 文件含义:
 */
@Component
@Aspect
public class PermAspect {

    /*

    * */

    @Autowired
    private TokenService tokenService;

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        joinPoint就是我们要执行的方法
        //拿到用户的权限 和 这个方法的权限比较
        HasPerm annotation = signature.getMethod().getAnnotation(HasPerm.class);
        if (annotation != null) {
            String s = annotation.permSign();
            boolean b = tokenService.getCacheLoginAdmin().getMenuList().stream().anyMatch(menu -> menu.getPermSign().equals(s));
            if (!b) {
                throw new ApiException(AxiosStatus.NO_PERM);
            }

        }

    }

    @Pointcut("execution(* com.shangma.cn.controller.*.*(..))")
    public void pointCut() {

    }
}
